﻿using System;
using System.Collections.Generic;
using Engine;
using Microsoft.Xna.Framework;
using ThinkDotNetEngine.Bots.Ammos;

namespace ThinkDotNetEngineDrawer.Bots.Ammos
{
    public class MasterAmmoDrawer : DrawableGameComponent
    {   
        readonly private Dictionary<Type,AmmoDrawerBase[]>  _ammoDrawers=new Dictionary<Type, AmmoDrawerBase[]>();

        public MasterAmmoDrawer(Game game)
            : base(game)
        {

        }

        #region Override

        public override void Initialize()
        {
            World = (World) Game.Services.GetService(typeof (World));
            AmmoHandler = (AmmoHandler) Game.Services.GetService(typeof (IAmmoHandler));
            InitAmmoDrawers();
            base.Initialize();
        }
        public override void Update(GameTime gameTime)
        {
            AmmoHandler.UpdateAll(gameTime);
            base.Update(gameTime);
        }
        public override void Draw(GameTime gameTime)
        {      
            foreach (AmmoBase ammo in AmmoHandler.LaunchedAmmos)
            {
                foreach (var ammoDrawerBase in _ammoDrawers[ammo.GetType()])
                {
                    ammoDrawerBase.DrawEffects(gameTime, ammo);
                }             
            }
            base.Draw(gameTime);
        }

        #endregion

        #region Private methods

        private void InitAmmoDrawers()
        {
            AddPlasmaDrawer();            
            AddRocketAmmoDrawer();
            AddTacticalMissileDrawer();

            foreach (var ammoDrawerBase in _ammoDrawers.Values)
            {
                foreach (var ammoDrawer in ammoDrawerBase)
                {
                    ammoDrawer.Initialize();
                }
            }
        }

        private void AddPlasmaDrawer()
        {
            _ammoDrawers.Add(typeof (PlasmaAmmo),
                             new AmmoDrawerBase[] {new TextureAmmoDrawer(Game, typeof (PlasmaAmmo))});
        }

        private void AddRocketAmmoDrawer()
        {
            _ammoDrawers.Add(typeof (RocketAmmo),
                             new AmmoDrawerBase[]
                                 {
                                     new ModelAmmoDrawer(Game, typeof (RocketAmmo)),
                                     new RocketParticleEffectsDrawer(Game, typeof (RocketAmmo))
                                 });
        }

        private void AddTacticalMissileDrawer()
        {
            _ammoDrawers.Add(typeof (TacticalMissileAmmo),
                             new AmmoDrawerBase[]
                                 {
                                     new ModelAmmoDrawer(Game, typeof (TacticalMissileAmmo)),
                                     new MissileEffectsAmmoDrawer(Game, typeof (TacticalMissileAmmo))
                                 });
        }

        #endregion

        #region Properties

        public World World { get; private set; }
        public AmmoHandler AmmoHandler { get; private set; }        

        #endregion

     
    }
}
